package com.demo.androidtv.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.demo.androidtv.R;
import com.demo.androidtv.bean.Book;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;


/**
 * Created by kevinLiao on 2017/7/1.
 */
@ContentView(R.layout.fragment_book_detail)
public class BookDetailFragment extends Fragment {

    @ViewInject(R.id.img_cover)
    ImageView imgCover;
    @ViewInject(R.id.tv_name)
    TextView tvName;
    @ViewInject(R.id.tv_author)
    TextView tvAuthor;
    @ViewInject(R.id.tv_publishing_house)
    TextView tvPublishingHouse;
    @ViewInject(R.id.tv_publish_time)
    TextView tvPublishTime;
    @ViewInject(R.id.tv_isbn)
    TextView tvIsbn;
    @ViewInject(R.id.tv_location)
    TextView tvLocation;
    @ViewInject(R.id.tv_description)
    TextView tvDescription;

    private Book mBook;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null)
            mBook = (Book) bundle.getSerializable("book");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_book_detail, container, false);
        x.view().inject(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        if (mBook != null) {
            setData(mBook);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    public void setData(Book book) {
        imgCover.setImageResource(book.getImgRes());
        tvName.setText(getString(R.string.label_book_name, book.getName()));
        tvAuthor.setText(getString(R.string.label_book_author, book.getAuthor()));
        tvPublishingHouse.setText(getString(R.string.label_book_publishing_house, book.getPublishingHouse()));
        tvPublishTime.setText(getString(R.string.label_book_publish_time, book.getPublishTime()));
        tvIsbn.setText(getString(R.string.label_book_isbn, book.getIsbn()));
        tvLocation.setText(book.getLocation());
        tvDescription.setText(book.getDescription());
    }
}
